package cave;

import java.util.ArrayList;

import android.graphics.Color;
import cave.game.GameServices;
import cave.game.InputEvent;
import cave.layer.Layer;
import cave.layer.LayerManager;
import cave.renderer.RenderList;
import cave.ui.Button;
import cave.ui.ButtonListener;
import cave.ui.RadioButton;
import cave.ui.RadioButtonGroup;
import cave.ui.RadioButtonGroupListener;
import cave.ui.TextField;
import cave.ui.UiContainer;

public class OptionsMenuLayer implements Layer {
	
	public final static String LAYER_ID = "OptMenu";
	
	private final LayerManager layerMgr;
	private GameServices gameServices;
	private UiContainer container;
	
	private TextField tfSelectText, tfSelectNro;
	
	public OptionsMenuLayer(final LayerManager layerMgr, GameServices gameServices) {
		this.layerMgr = layerMgr;
		this.gameServices = gameServices;
	}

	@Override
	public void onCreate() {
		container = new UiContainer();
		
		container.add(new TextField(0.0f, 0.9f, "OptionsMenuLayer",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.GREEN));
		
		tfSelectText = new TextField(0.0f, 0.8f, "Selection: ",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.RED);
		container.add(tfSelectText);
		
		tfSelectNro = new TextField(tfSelectText.getLength(), 0.8f, "none",
				gameServices.getTextureManager().getFont("medium"), 0.1f, Color.BLUE);
		container.add(tfSelectNro);
		
		container.add(new Button(0.1f, 0.1f, 0.2f, 0.2f,
				gameServices.getTextureManager().getTexture("but_g_up"),
				gameServices.getTextureManager().getTexture("but_g_down"),
				gameServices.getTextureManager().getTexture("but_g_dis"),
				new ButtonListener() {
					@Override
					public void actionClick() {
						layerMgr.openLayer(MainMenuLayer.LAYER_ID);
						layerMgr.closeLayer(LAYER_ID);
					}
				}
		));
		
		RadioButtonGroup group = new RadioButtonGroup(
				new RadioButtonGroupListener() {
					@Override
					public void onSelectionChange(int oldIndex, int newIndex) {
						tfSelectNro.setInt(newIndex);
					}
				}
		);
		container.add(group);
		
		group.addRadioButton(new RadioButton(1.0f, 0.65f, 0.1f, 0.1f,
				gameServices.getTextureManager().getTexture("but_rad_se"),
				gameServices.getTextureManager().getTexture("but_rad_uns"),
				gameServices.getTextureManager().getTexture("but_rad_act"),
				gameServices.getTextureManager().getTexture("but_rad_dis")));
		
		group.addRadioButton(new RadioButton(1.0f, 0.5f, 0.1f, 0.1f,
				gameServices.getTextureManager().getTexture("but_rad_se"),
				gameServices.getTextureManager().getTexture("but_rad_uns"),
				gameServices.getTextureManager().getTexture("but_rad_act"),
				gameServices.getTextureManager().getTexture("but_rad_dis")));
		
		group.addRadioButton(new RadioButton(1.0f, 0.35f, 0.1f, 0.1f,
				gameServices.getTextureManager().getTexture("but_rad_se"),
				gameServices.getTextureManager().getTexture("but_rad_uns"),
				gameServices.getTextureManager().getTexture("but_rad_act"),
				gameServices.getTextureManager().getTexture("but_rad_dis")));
		
		group.addRadioButton(new RadioButton(1.0f, 0.2f, 0.1f, 0.1f,
				gameServices.getTextureManager().getTexture("but_rad_se"),
				gameServices.getTextureManager().getTexture("but_rad_uns"),
				gameServices.getTextureManager().getTexture("but_rad_act"),
				gameServices.getTextureManager().getTexture("but_rad_dis")));
	}

	@Override
	public void onOpen() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpdate(float dt, ArrayList<InputEvent> inputEvents) {
		container.update(dt, inputEvents);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onClose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDestroy() {
		container.destroy();
	}

	@Override
	public void draw(RenderList renderList) {
		container.draw(renderList);
	}

	@Override
	public String getId() {
		return LAYER_ID;
	}
 
}

